package org.jivesoftware.smackx.packet;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smack.packet.PacketExtension;

public class Bytestream extends IQ
{
  private Mode mode = Mode.tcp;
  private String sessionID;
  private final List<StreamHost> streamHosts = new ArrayList();
  private Activate toActivate;
  private StreamHostUsed usedHost;

  public Bytestream(String paramString)
  {
    setSessionID(paramString);
  }

  public StreamHost addStreamHost(String paramString1, String paramString2)
  {
    return addStreamHost(paramString1, paramString2, 0);
  }

  public StreamHost addStreamHost(String paramString1, String paramString2, int paramInt)
  {
    StreamHost localStreamHost = new StreamHost(paramString1, paramString2);
    localStreamHost.setPort(paramInt);
    addStreamHost(localStreamHost);
    return localStreamHost;
  }

  public void addStreamHost(StreamHost paramStreamHost)
  {
    this.streamHosts.add(paramStreamHost);
  }

  public int countStreamHosts()
  {
    return this.streamHosts.size();
  }

  public String getChildElementXML()
  {
    String str;
    Iterator localIterator2;
    StringBuilder localStringBuilder = new StringBuilder();
    localStringBuilder.append("<query xmlns=\"http://jabber.org/protocol/bytestreams\"");
    if (getType().equals(IQ.Type.SET))
    {
      if (getSessionID() != null)
        localStringBuilder.append(" sid=\"").append(getSessionID()).append("\"");
      if (getMode() != null)
        localStringBuilder.append(" mode = \"").append(getMode()).append("\"");
      localStringBuilder.append(">");
      if (getToActivate() == null)
      {
        localIterator2 = getStreamHosts().iterator();
        if (!(localIterator2.hasNext()))
        {
          localStringBuilder.append("</query>");
          str = localStringBuilder.toString();
        }
      }
    }
    while (true)
    {
      do
        while (true)
        {
          while (true)
          {
            while (true)
            {
              return str;
              localStringBuilder.append(((StreamHost)localIterator2.next()).toXML());
            }
            localStringBuilder.append(getToActivate().toXML());
          }
          if (!(getType().equals(IQ.Type.RESULT)))
            break label256;
          localStringBuilder.append(">");
          if (getUsedHost() == null)
            break;
          localStringBuilder.append(getUsedHost().toXML());
        }
      while (countStreamHosts() <= 0);
      Iterator localIterator1 = this.streamHosts.iterator();
      while (true)
      {
        if (!(localIterator1.hasNext()));
        localStringBuilder.append(((StreamHost)localIterator1.next()).toXML());
      }
      label256: str = null;
    }
  }

  public Mode getMode()
  {
    return this.mode;
  }

  public String getSessionID()
  {
    return this.sessionID;
  }

  public StreamHost getStreamHost(String paramString)
  {
    Object localObject;
    if (paramString == null)
      localObject = null;
    while (true)
    {
      StreamHost localStreamHost;
      return localObject;
      Iterator localIterator = this.streamHosts.iterator();
      do
      {
        while (!(localIterator.hasNext()))
          localObject = null;
        localStreamHost = (StreamHost)localIterator.next();
      }
      while (!(localStreamHost.getJID().equals(paramString)));
      localObject = localStreamHost;
    }
  }

  public Collection<StreamHost> getStreamHosts()
  {
    return Collections.unmodifiableCollection(this.streamHosts);
  }

  public Activate getToActivate()
  {
    return this.toActivate;
  }

  public StreamHostUsed getUsedHost()
  {
    return this.usedHost;
  }

  public void setMode(Mode paramMode)
  {
    this.mode = paramMode;
  }

  public void setSessionID(String paramString)
  {
    this.sessionID = paramString;
  }

  public void setToActivate(String paramString)
  {
    this.toActivate = new Activate(paramString);
  }

  public void setUsedHost(String paramString)
  {
    this.usedHost = new StreamHostUsed(paramString);
  }

  public static class Activate
  implements PacketExtension
  {
    public static String ELEMENTNAME = "activate";
    public String NAMESPACE = "";
    private final String target;

    public Activate(String paramString)
    {
      this.target = paramString;
    }

    public String getElementName()
    {
      return ELEMENTNAME;
    }

    public String getNamespace()
    {
      return this.NAMESPACE;
    }

    public String getTarget()
    {
      return this.target;
    }

    public String toXML()
    {
      StringBuilder localStringBuilder = new StringBuilder();
      localStringBuilder.append("<").append(getElementName()).append(">");
      localStringBuilder.append(getTarget());
      localStringBuilder.append("</").append(getElementName()).append(">");
      return localStringBuilder.toString();
    }
  }

  public static enum Mode
  {
    static
    {
      Mode[] arrayOfMode = new Mode[2];
      arrayOfMode[0] = tcp;
      arrayOfMode[1] = udp;
      ENUM$VALUES = arrayOfMode;
    }

    public static Mode fromName(String paramString)
    {
      Mode localMode1;
      Mode localMode2;
      try
      {
        localMode2 = valueOf(paramString);
        localMode1 = localMode2;
        return localMode1;
      }
      catch (Exception localException)
      {
        localMode1 = tcp;
      }
    }
  }

  public static class StreamHost
  implements PacketExtension
  {
    public static String ELEMENTNAME;
    public static String NAMESPACE = "";
    private final String JID;
    private final String addy;
    private int port = 0;

    static
    {
      ELEMENTNAME = "streamhost";
    }

    public StreamHost(String paramString1, String paramString2)
    {
      this.JID = paramString1;
      this.addy = paramString2;
    }

    public String getAddress()
    {
      return this.addy;
    }

    public String getElementName()
    {
      return ELEMENTNAME;
    }

    public String getJID()
    {
      return this.JID;
    }

    public String getNamespace()
    {
      return NAMESPACE;
    }

    public int getPort()
    {
      return this.port;
    }

    public void setPort(int paramInt)
    {
      this.port = paramInt;
    }

    public String toXML()
    {
      StringBuilder localStringBuilder = new StringBuilder();
      localStringBuilder.append("<").append(getElementName()).append(" ");
      localStringBuilder.append("jid=\"").append(getJID()).append("\" ");
      localStringBuilder.append("host=\"").append(getAddress()).append("\" ");
      if (getPort() != 0)
        localStringBuilder.append("port=\"").append(getPort()).append("\"");
      while (true)
      {
        localStringBuilder.append("/>");
        return localStringBuilder.toString();
        localStringBuilder.append("zeroconf=\"_jabber.bytestreams\"");
      }
    }
  }

  public static class StreamHostUsed
  implements PacketExtension
  {
    public static String ELEMENTNAME = "streamhost-used";
    private final String JID;
    public String NAMESPACE = "";

    public StreamHostUsed(String paramString)
    {
      this.JID = paramString;
    }

    public String getElementName()
    {
      return ELEMENTNAME;
    }

    public String getJID()
    {
      return this.JID;
    }

    public String getNamespace()
    {
      return this.NAMESPACE;
    }

    public String toXML()
    {
      StringBuilder localStringBuilder = new StringBuilder();
      localStringBuilder.append("<").append(getElementName()).append(" ");
      localStringBuilder.append("jid=\"").append(getJID()).append("\" ");
      localStringBuilder.append("/>");
      return localStringBuilder.toString();
    }
  }
}